extends Node
class_name InputAdapter

signal action_requested(action: String, pos: Vector2)

var current_tool := "hoe"

func _ready():
	set_process_unhandled_input(true)
	print("InputAdapter 已初始化")

func _unhandled_input(event: InputEvent):
	# 鼠标交互
	if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
		print("检测到鼠标左键点击: ", event.position)
		action_requested.emit("interact", event.position)
		get_viewport().set_input_as_handled()
	
	# 触摸交互
	if event is InputEventScreenTouch and event.pressed:
		print("检测到触摸输入: ", event.position)
		action_requested.emit("interact", event.position)
		get_viewport().set_input_as_handled()

func _input(event: InputEvent):
	# 工具切换
	if event.is_action_pressed("tool_hoe"):
		set_current_tool("hoe")
	elif event.is_action_pressed("tool_water"):
		set_current_tool("water")
	elif event.is_action_pressed("tool_seed_1"):
		set_current_tool("seed_turnip")
	elif event.is_action_pressed("tool_seed_2"):
		set_current_tool("seed_potato")
	elif event.is_action_pressed("tool_seed_3"):
		set_current_tool("seed_corn")
	elif event.is_action_pressed("tool_harvest"):
		set_current_tool("harvest")
	
	# UI 切换
	elif event.is_action_pressed("open_inventory"):
		action_requested.emit("open_inventory", Vector2.ZERO)
	elif event.is_action_pressed("open_shop"):
		action_requested.emit("open_shop", Vector2.ZERO)
	elif event.is_action_pressed("pause"):
		action_requested.emit("pause", Vector2.ZERO)

func set_current_tool(tool: String):
	current_tool = tool
	action_requested.emit("tool_changed", Vector2.ZERO)

func get_current_tool() -> String:
	return current_tool

# 供UI按钮调用的方法
func on_tool_button_pressed(tool: String):
	set_current_tool(tool)

func on_ui_button_pressed(action: String):
	action_requested.emit(action, Vector2.ZERO)